<template>
  <el-container class="home">
    <!-- 头部 -->
    <el-header>
      <div>
        <img src="../assets/raindrop.png" alt=""/>
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="menuCollapsed ? '64px' : '200px'">
        <div class="btn-toggle" @click="toggleMenuCollapsed">|||</div>
        <!-- 为了保证类型为 boolean，必须对 collapse-transition 使用 Vue 的数据绑定 -->
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
          unique-opened
          :collapse="menuCollapsed"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu v-for="menu in menus" :key="menu.id" :index="String(menu.id)">
            <template slot="title">
              <i :class="iconMap[menu.id]"></i>
              <span>{{ menu.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              v-for="submenu in menu.children"
              :key="submenu.id"
              :index="'/' + submenu.path"
              @click="setActivePath('/' + submenu.path)"
            >
              <i class="el-icon-menu"></i>
              <span>{{ submenu.authName }}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主体 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      menus: [],
      iconMap: {
        '125': 'iconfont icon-user',
        '103': 'iconfont icon-tijikongjian',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao'
      },
      menuCollapsed: false,
      activePath: ''
    }
  },
  created () {
    this.getMenus()
    this.getActivePath()
  },
  methods: {
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getMenus () {
      const { data } = await this.$http.get('menus')
      if (data.meta.status !== 200) {
        return this.$message.error(data.meta.msg)
      }
      this.menus = data.data
    },
    toggleMenuCollapsed () {
      this.menuCollapsed = !this.menuCollapsed
    },
    getActivePath () {
      this.activePath = window.sessionStorage.getItem('activePath')
    },
    setActivePath (path) {
      this.activePath = path
      window.sessionStorage.setItem('activePath', path)
    }
  }
}
</script>

<style lang="less" scoped>
.home {
  height: 100%;
}
.el-header {
  background: #373d41;
  color: #fff;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    > img {
      height: 40px;
      border-radius: 20px;
    }
    > span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background: #333744;
  color: #fff;
}
.el-main {
  background: #eaedf1;
}
.el-menu {
  border-right: none;
}
.iconfont {
  margin-right: 10px;
}
.btn-toggle {
  background: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
  user-select: none;
}
</style>
